package io.renren.modules.pointhisdata.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.pointhisdata.dao.PointDataTestDao;
import io.renren.modules.pointhisdata.entity.PointDataTestEntity;
import io.renren.modules.pointhisdata.service.PointDataTestService;


@Service("pointDataTestService")
public class PointDataTestServiceImpl extends ServiceImpl<PointDataTestDao, PointDataTestEntity> implements PointDataTestService {

    @Autowired
    private PointDataTestDao pointDataTestDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PointDataTestEntity> page = this.page(
                new Query<PointDataTestEntity>().getPage(params),
                new QueryWrapper<PointDataTestEntity>().like("gwSN",params.get("key"))
//                new QueryWrapper<PointDataTestEntity>().le("time","");
        );

        return new PageUtils(page);
    }



    @Override
    public void exportAllDataInBatches() {
        int batchSize = 5000; // 每批次查询的数据量
        int totalCount = pointDataTestDao.getTotalCount();
        int totalBatches = (int) Math.ceil((double) totalCount / batchSize);

        try (SXSSFWorkbook workbook = new SXSSFWorkbook()) {
            //记录当前时间
            long beginTime = System.currentTimeMillis();
            SXSSFSheet sheet = workbook.createSheet("PointData");

            Row headerRow = sheet.createRow(0);
            String[] headers = {"类型", "上报时间", "网关号", "仪表号", "仪表名", "仪表状态", "电压", "电流", "功率", "温度", "正向有功电能", "反向有功电能", "电流变比", "电压变比"};
            for (int i = 0; i < headers.length; i++) {
                Cell cell = headerRow.createCell(i);
                cell.setCellValue(headers[i]);
            }

            int rowIdx = 1; // 从第二行开始写入数据
            for (int i = 0; i < totalBatches; i++) {
                int start = i * batchSize;
                List<PointDataTestEntity> dataBatch = pointDataTestDao.getDataByPage(start, batchSize);

                System.out.println(dataBatch);

                System.out.println("正在写入第" + (i + 1) + "批数据，数据量：" + dataBatch.size());
                for (PointDataTestEntity data : dataBatch) {
                    Row row = sheet.createRow(rowIdx++);
                    row.createCell(0).setCellValue(data.getType());
                    row.createCell(1).setCellValue(data.getTime());
                    row.createCell(2).setCellValue(data.getGwsn());
                    row.createCell(3).setCellValue(data.getMetersn());
                    row.createCell(4).setCellValue(data.getMetername());
                    row.createCell(5).setCellValue(data.getMeterstatus());
                    row.createCell(6).setCellValue(data.getU().doubleValue());
                    row.createCell(7).setCellValue(data.getI().doubleValue());
                    row.createCell(8).setCellValue(data.getP().doubleValue());
                    row.createCell(9).setCellValue(data.getTemp());
                    row.createCell(10).setCellValue(data.getEpi().doubleValue());
                    row.createCell(11).setCellValue(data.getEpe().doubleValue());
                    row.createCell(12).setCellValue(data.getPt());
                    row.createCell(13).setCellValue(data.getCt());
                }

                // 清空dataBatch列表
                dataBatch.clear();
                System.out.println(dataBatch);
                System.out.println("第" + (i + 1) + "批数据写入完毕");
                //当前数据是第几条数据
                System.out.println("当前数据是第" + (i + 1) * batchSize + "条数据");
                // 在批次结束后，显式调用垃圾回收
                System.gc();


            }

            try (FileOutputStream outputStream = new FileOutputStream("/Users/w/Desktop/leetcode/data-export.xlsx")) {
                workbook.write(outputStream);
            }

            //结束时间
            long endTime = System.currentTimeMillis();
            System.out.println("总共耗时：" + (endTime - beginTime) / 1000 + "秒");


        } catch (IOException e) {
            e.printStackTrace();
            // 处理异常
        }
    }




}